package com.lagou.mapper;

import com.lagou.connect.ConectionManager;
import com.lagou.connect.TransactionIsolationLevel;
import com.lagou.pojo.Configuration;
import com.lagou.sqlSession.DefaultSqlSession;
import com.lagou.sqlSession.Executor;
import com.lagou.sqlSession.SimpleExecutor;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;

/**
 * @author: wudong
 * @create: 2021-05-31 14:09
 **/
public class SpringSqlSessionFactory implements SqlSessionFactory {

    private Configuration configuration;

    public SpringSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public SqlSession openSession() {
        return openSession(true);
    }

    @Override
    public SqlSession openSession(boolean autoCommit) {
        return openSession(null,autoCommit);
    }

    @Override
    public SqlSession openSession(TransactionIsolationLevel level, boolean autoCommit) {
        ConectionManager conectionManager = new SpringConectionManager(configuration.getDataSource(),level, autoCommit);
        Executor executor = new SimpleExecutor(conectionManager);
        return new DefaultSqlSession(configuration,executor);
    }

    @Override
    public Configuration getConfiguration() {
        return configuration;
    }
}
